/******************************************************************************
* File description: LabelTextInput.qml : BaseTextInput.qml
* Function: 带标签的Input，继承自TextInput基类
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-07
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14

Item {
    id: root

    /* property 定义开始 */
    // 标签
    property string labelText: "-- LabelTextInput Default --"
    property string labelFontColor: "#222"
    property int labelFontPixelSize: 16
    property string labelFontFamily: "Alibaba PuHuiTi"
    property int labelHorAlign: Qt.AlignLeft // 标签水平对齐方式

    // 输入框
    property int inputWidth: 273
    property int inputHeight: 42
    property string borderColor: "#999"
    property int inputBorderRadius: 4
    property string inputBgColor: "#FFF" // 输入框背景颜色
    property string errorBorderColor: "red" // 错误边框提示
    property var rulesArr: [] // Input规则数组（填入GLOBAL的Rules枚举）
    property string inputText: "" // 输入框文字
    property string defaultInputText: "" // 输入框默认文字
    property bool inputReadOnly: false

    // 文字
    property int inputFontPixelSize: 16
    property string inputFontColor: "#222"
    property string inputFontFamily: "Alibaba PuHuiTi"

    // placeholder
    property string placeholderText: ""
    property string placeholderColor: "#FF999999"

    // 错误提示标签
    property bool errorTipsIsVisible: false
    property string errorTipsText: ""

    // 布局
    // 标签排列方式
    property int labelPosition: GLOBAL.LabelPosition.Left // 默认左右布局
    property int gridRowCount: _getGirdRowNCol(labelPosition)[0]  // 网格布局行数
    property int gridColCount: _getGirdRowNCol(labelPosition)[1] // 网格布局列数
    property int rowSpacing: 0
    property int colSpacing: 9

    // 不可用状态
    property string disabledBgColor: "#F5F5F5"
    property string disabledBorderColor: "#999"
    property string disabledFontColor: "#BFBFBF"

    // private
    property string _errorTipsFontColor: "red" // 错误提示文字颜色
    property int _errorTipsFontPixelSize: 15 // 错误提示文字大小
    property string _errorTipsFontFamily: "Alibaba PuHuiTi" // 错误提示字体簇
    // 标签宽高
    property int _InputTextLabelWidth: 0
    property int _InputTextLabelHeight: 0
    // 设置的标签宽高
    property int _setInputTextLabelWidth: 0
    property int _setInputTextLabelHeight: 0

    // 信号
    signal errorTipsVisible(int errTipsWidth, int errTipsHeight); // 当错误提示显示变化时
    signal editingFinished(string text);
    signal textEdited(string text);

    // 函数
    /*
      获取网格布局行列数
      @param {int} labelPosition 布局方式
      @return {list<int, int>} 网格布局行列数: [row, col]
     */
    property var _getGirdRowNCol: function(labelPosition){
        // 左右布局
        if(labelPosition === GLOBAL.LabelPosition.Left){
            return [1, 2]
        }
        // 上下布局
        else if(labelPosition === GLOBAL.LabelPosition.Top){
            return [2, 1]
        }
    }

    /*
     * 获取标签宽高
     * @return {Array} 标签宽高
     */
    function getLabelSize(){
        return [_InputTextLabelWidth, _InputTextLabelHeight]
    }

    /*
     * 设置标签宽高
     * @param {int} width 标签宽度
     * @param {int} height 标签高度
     */
    function setLabelSize(width, height){
        if(width){
            _setInputTextLabelWidth = width
            baseLabelCon.Layout.preferredWidth = _setInputTextLabelWidth
        }
        if(height){
            _setInputTextLabelHeight = height
            baseLabelCon.Layout.preferredHeight = _setInputTextLabelHeight
        }
    }

    // 设置文本内容
    function setInputText(text){
        baseInput.setInputText(text)
    }
    /* property 定义结束 */

    ColumnLayout {
        id: colLayout
        anchors.fill: parent
        spacing: 0

        CustomVerSpacerItem {}

        // 输入框+标签
        Item {
            Layout.fillWidth: true
            Layout.preferredHeight: baseInput.height
            Layout.alignment: Qt.AlignVCenter | labelHorAlign

            GridLayout {
                id: gridLayout
                anchors.fill: parent
                rows: gridRowCount
                columns: gridColCount
                rowSpacing: root.rowSpacing
                columnSpacing: root.colSpacing

                // 标签
                Item {
                    id: baseLabelCon
                    Layout.preferredWidth: baseLabel.width
                    Layout.preferredHeight: baseLabel.height
                    Layout.alignment: Qt.AlignVCenter | Qt.AlignLeft

                    Label {
                        id: baseLabel
                        text: labelText
                        font.pixelSize: labelFontPixelSize
                        font.family: labelFontFamily
                        color: labelFontColor

                        // 必填
                        Label {
                            id: requiredStar
                            visible: rulesArr.indexOf(GLOBAL.Rules.Required) !== -1
                            anchors.right: parent.left
                            anchors.top: parent.top
                            anchors.bottom: parent.bottom
                            font.pixelSize: 17
                            font.family: "Alibaba PuHuiTi"
                            color: "#FF0000"
                            text: "* "
                        }

                        onWidthChanged: {
                            _InputTextLabelWidth = width
                        }

                        onHeightChanged: {
                            _InputTextLabelHeight = height
                        }
                    }
                }

                // 输入框
                Item {
                    id: baseInputCon
                    Layout.fillWidth: true
                    Layout.preferredWidth: inputWidth
                    Layout.preferredHeight: inputHeight
                    Layout.alignment: Qt.AlignCenter

                    BaseTextInput {
                        id: baseInput
                        inputBorderColor: errorTipsIsVisible ? errorBorderColor : borderColor
                        anchors.fill: parent
                        inputText: root.inputText
                        isReadOnly: inputReadOnly
                        inputRadius: inputBorderRadius
                        inputFontPixelSize: root.inputFontPixelSize
                        inputFontColor: root.inputFontColor
                        inputFontFamily: root.inputFontFamily
                        inputBgColor: root.inputBgColor
                        disabledBgColor: root.disabledBgColor
                        disabledBorderColor: root.disabledBorderColor
                        disabledFontColor: root.disabledFontColor
                        placeholderText: root.placeholderText
                        placeholderColor: root.placeholderColor

                        // 信号
                        onTextEdited: function(text){
                            // 如果匹配规则：必填
                            if(rulesArr.indexOf(GLOBAL.Rules.Required) !== -1){
                                // 如果字符长度为0，则弹出报错信息
                                if(text.length === 0){
                                    errorTipsIsVisible = true
                                    errorTipsText = labelText + "不能为空"
                                }else{
                                    errorTipsIsVisible = false
                                    errorTipsText = ""
                                }
                            }
                            root.textEdited(text);
                        }
                        onEditingFinished: function(text){
                            // 保存输入内容
                            root.inputText = text
                            root.editingFinished(text)
                        }
                    }
                }
            }
        }

        // 错误提示
        Item {
            Layout.fillWidth: true
            Layout.leftMargin: baseLabel.width + (labelPosition === GLOBAL.LabelPosition.Left ? colSpacing : 0)
            Layout.preferredHeight: errorTips.height
            Layout.alignment: Qt.AlignLeft | Qt.AlignTop
            visible: errorTipsIsVisible // 默认不可见

            Label {
                id: errorTips
                font.family: _errorTipsFontFamily
                font.pixelSize: _errorTipsFontPixelSize
                color: _errorTipsFontColor
                text: errorTipsText
                verticalAlignment: Text.AlignTop

                // 如果错误信息可见，则输入框边框变色
                onVisibleChanged: {
                    if(errorTipsIsVisible){
                        baseInput.inputBorderColor = errorBorderColor
                        root.errorTipsVisible(errorTips.width,
                                              errorTips.height + colLayout.spacing)
                    }
                    else {
                        baseInput.inputBorderColor = borderColor
                        root.errorTipsVisible(-errorTips.width,
                                              -(errorTips.height + colLayout.spacing))
                    }
                }
            }
        }

        CustomVerSpacerItem {}
    }
}
